package com.hera.process.config.service.impl;


import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hera.base.util.FiltroDTO;
import com.hera.process.config.dao.StageDao;
import com.hera.process.config.dao.StageRollupDao;
import com.hera.process.config.service.StageRollupService;
import com.hera.process.workflow.domain.Stage;
import com.hera.process.workflow.domain.StageRollup;


@Service("stagerollupservice")
public class StageRollupServiceImpl implements 	StageRollupService {

	static final Logger logger = Logger.getLogger(StageRollupServiceImpl.class);
	
	@Autowired
	private StageRollupDao stagerollupdao;
	
	@Autowired
	private StageDao stagedao;

	@Override
	public StageRollup findById(Long id) {
		
		return stagerollupdao.findOne(id);
	}

	@Override
	public void update(StageRollup entity) {
		if(entity.getStageFrom() != null){
			if(entity.getIdFigureFrom() != null){
				List<Stage> listStage = stagedao.findStageByProcessAndIdDiagram(entity.getIdProcess(), entity.getIdFigureFrom());
				if(listStage != null && listStage.size()>0){
					entity.setStageFrom(listStage.get(0));
				}
			}else{
				entity.setStageFrom(null);
			}
			if(entity.getIdFigureTo() != null){
				List<Stage> listStage1 = stagedao.findStageByProcessAndIdDiagram(entity.getIdProcess(), entity.getIdFigureTo());
				if(listStage1 != null && listStage1.size()>0){
					entity.setStageTo(listStage1.get(0));
				}
			}else{
				entity.setStageTo(null);
			}
		}
		
		 stagerollupdao.save(entity);
	}

	@Override
	public void save(StageRollup entity) {
		if(entity.getStageFrom() != null){
			List<Stage> listStage = stagedao.findStageByProcessAndIdDiagram(entity.getStageFrom().getSwimlane().getSwimlanePK().getIdProcess(), entity.getIdFigureFrom());
			if(listStage != null && listStage.size()>0){
				entity.setStageFrom(listStage.get(0));
			}
			if(entity.getIdFigureTo() != null){
				List<Stage> listStage1 = stagedao.findStageByProcessAndIdDiagram(entity.getStageFrom().getSwimlane().getSwimlanePK().getIdProcess(), entity.getIdFigureTo());
				if(listStage1 != null && listStage1.size()>0){
					entity.setStageTo(listStage1.get(0));
				}
			}
		}
		
		stagerollupdao.save(entity);
	}

	@Override
	public void delete(StageRollup entity) {
		
		stagerollupdao.delete(entity);
	}

	@Override
	public List<StageRollup> findAll() {
		
		return stagerollupdao.findAll();
	}

	@Override
	public List<StageRollup> findAll(FiltroDTO filtroDTO) {
		
		return stagerollupdao.obtenerEntidadesPorFiltros(filtroDTO);
	}

	@Override
	public List<StageRollup> findStageRollupByProcessId(Long idProcess) {
		
		return stagerollupdao.findStageRollupByProcessId(idProcess);
	}

	@Override
	public List<StageRollup> findStageRollupByProcessAndIdDiagram(
			Long idProcess, String idDiagram) {
		
		return stagerollupdao.findByIdProcessAndIdFigure(idProcess, idDiagram);
	}
	
	

}
